package com.zhida.chatservice.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zhida.chatservice.service.WsService;
import com.zhida.common.entity.Result;
import com.zhida.common.entity.WsGroup;
import com.zhida.common.utils.TokenProcessor;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * Create by zhh 2024/7/16 7:46
 */
@RestController
@RequestMapping("/ws")
public class WsController {

    @Resource
    final WsService wsService;

    public WsController(WsService wsService) {
        this.wsService = wsService;
    }

    @GetMapping("/userOnline/{username}")
    public Result<?> userOnline(@PathVariable String username){
        return wsService.userOnline(username);
    }
    @PostMapping("/createGroup")
    public Result<?> createGroup(@RequestBody WsGroup wsGroup,HttpServletRequest request){
        return wsService.createGroup(wsGroup,TokenProcessor.getUsername(request));
    }
    @GetMapping("/joinGroup/{group_id}")
    public Result<?> joinGroup(@PathVariable Integer group_id,HttpServletRequest request){
        return wsService.joinGroup(group_id,TokenProcessor.getUsername(request));
    }
    @GetMapping("/getGroupsInfo")
    public Result<?> getGroupsInfo(){
        return wsService.getGroupsInfo();
    }
    @PostMapping("/updateGroupInfo")
    public Result<?> updateGroupInfo(@RequestBody HashMap<String,String> data){
        return wsService.updateGroupInfo(data);
    }
    @GetMapping("/getGroupMembers/{group_id}")
    public Result<?> getGroupMembers(@PathVariable String group_id){
        return wsService.getGroupMembers(group_id);
    }
    @PostMapping("/sendImages")
    public Result<?> sendImage(@RequestParam(value = "images",required = false) List<MultipartFile> files, @RequestParam(value="formData") String jsonFormData){
        return wsService.sendImages(files,jsonFormData);
    }
}
